﻿using System;
using System.Reflection;
using Windows.UI.Xaml.Data;
using XamlInspector.Foundation;

namespace XamlInspector.Model.Properties
{
    public class XamlEnumProperty : XamlProperty
    {
        public XamlEnumProperty(PropertyInfo propertyInfo, object element)
            : base(propertyInfo, element)
        {
            try
            {
                var values = GetListValues();

                values.MoveCurrentTo(propertyInfo.GetValue(element).ToString());
                values.CurrentChanged += (s, e) =>
                {
                    if (values.CurrentItem != null)
                    {
                        propertyInfo.SetValue(element, Enum.Parse(propertyInfo.PropertyType, values.CurrentItem.ToString()));
                    }
                };
                Values = values;
            }
            catch (Exception)
            {
            }
            
        }

        private ListCollectionView GetListValues()
        {
            return new ListCollectionView(GetNames());
        }

        protected virtual string[] GetNames()
        {
            return Enum.GetNames(PropertyInfo.PropertyType);
        }

        public ICollectionView Values { get; private set; }
    }
}
